package com.shawn.server.service;

import com.alibaba.fastjson.JSON;
import com.shawn.commons.utils.RC;
import com.shawn.model.vo.ErrorResult;
import com.shawn.server.exception.BusinessException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

@Service
public class FreezeService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void checkFreeze(Long userId,String freezeType) {
        if (!Objects.isNull(userId)){
            //查找redis
            String key = RC.FREEZING_USER+ userId;
            String mapJson = redisTemplate.opsForValue().get(key);
            if (!Strings.isEmpty(mapJson)){
                Map map = JSON.parseObject(mapJson, Map.class);
                String freezingRange = (String) map.get("freezingRange");
                String range = null;
                switch (freezingRange){
                    case "1":
                        range="冻结登录";
                        break;
                    case "2":
                        range="冻结发言";
                        break;
                    case "3":
                        range="冻结发布动态";
                        break;
                    default:
                        break;
                }
                String freezingTime = (String) map.get("freezingTime");
                String days ="";
                switch (freezingTime){
                    case "1":
                        days="3天";
                        break;
                    case "2":
                        days="7天";
                        break;
                    case "3":
                        days="永久";
                        break;
                    default:
                        throw new BusinessException(ErrorResult.builder().errCode("冻结时间参数错误!").build());
                }
                String reasonsForFreezing = (String) map.get("reasonsForFreezing");
                if (freezeType.equals(freezingRange)){
                    throw new BusinessException(ErrorResult.builder().errMessage("您因"+reasonsForFreezing+"已被"+range+":"
                            +days).build());
                }
            }

        }
    }
}
